﻿Option Strict On
Option Explicit On
Option Infer On

#Region "Imports"
Imports System
Imports System.Linq
Imports System.Xml.Linq
Imports System.Collections.Generic
Imports GenDotNet.TemplateData
Imports GenDotNet.TemplateSupport
Imports GenDotNet.TemplateSupport.Strings
#If CSharp Then
Imports GenDotNet.TemplateSupport.CSharp
#Else
Imports GenDotNet.TemplateSupport.VB
#End If
Imports GenDotNet.Task.Common
#End Region

Public Class EditableRootUserTemplate
	Inherits DotNetLanguageBase

#Region "Required methods with static contents"
	Public Sub New()
		MyBase.New()
	End Sub

	' NEWTEMPLATE: Change to new guid for every new template
	Private ReadOnly _templateGuid As New Guid("{ED757570-799F-45d4-B53A-A06D8BB8C9AB}")
	Public Overrides ReadOnly Property Id() As System.Guid
		Get
			Return _templateGuid
		End Get
	End Property

	Protected Overrides Function GetFileImports() As TemplateSupport.ImportInfoCollection
		Return New CslaTemplate.Support.CslaImportInfoCollection
	End Function

	Protected Overrides Function GenerateFile() As String
		' NEWTEMPLATE: Exactly this code
		Return _
<code>
	<%= Me.OutputFile() %>
</code>.Value.CleanOutput
	End Function

	Protected Overrides Function GetFileName(ByVal element As System.Xml.Linq.XElement) As String
		' NEWTEMPLATE: Update the following file name
		Return OutputFilename(element.@Name)
	End Function
#End Region

	Protected Overrides Function GenerateContents() As String
		Dim classInfo = New TypeInfo(ObjectData.ClassName, Scope.Public, TypeCategory.Class, TypeModifiers.Partial)
		Return _
	  <code>

    ''' &lt;summary>
    ''' This partial class is intended solely for handcrafted code.
    ''' The code you add here will either support CSLA (such as
    ''' additional validation) or provide new features (such as
    ''' a full name).
    ''' &lt;/summary>
    ''' &lt;remarks>
    ''' Do NOT add constructors to this class. All business
    ''' classes are created through factories in CSLA.
    ''' &lt;/remarks>
    &lt;Serializable> _
    Public Class <%= ObjectData.ClassName %>
        Inherits <%= ObjectData.ClassName %>Base(Of <%= ObjectData.ClassName %>)
        <%= GenerateValidationRules() %>
		  <%= GenerateAuthorizationRules() %>
		  <%= GenerateValidationRules() %>
		  <%= GenerateFactoryMethods() %>

        Protected Overrides Sub SetDefaults()
            LoadProperty(Of Guid)(IdProperty, Guid.NewGuid)
        End Sub

    End Class

End Namespace
</code>.Value
	End Function

	Private Function GenerateValidationRules() As String
		Return _
<code> 

    #Region " Validation Rules "

#End Region ' Validation Rules
</code>.Value
	End Function

	Private Function GenerateAuthorizationRules() As String
		Return _
<code> 

    #Region " Authorization Rules "

        Public Shared Function CanGetObject() As Boolean
            'TODO: Define CanGetObject permission in <%= ObjectData.ClassName %>
            Return True
            'If Csla.ApplicationContext.User.IsInRole("<%= ObjectData.ClassName %>ViewGroup") Then
            '	Return True
            'End If
            'Return False
        End Function

        Public Shared Function CanAddObject() As Boolean
            'TODO: Define CanAddObject permission in <%= ObjectData.ClassName %>
            Return True
            'If Csla.ApplicationContext.User.IsInRole("<%= ObjectData.ClassName %>AddGroup") Then
            '	Return True
            'End If
            'Return False
        End Function

        Public Shared Function CanEditObject() As Boolean
            'TODO: Define CanEditObject permission in <%= ObjectData.ClassName %>
            Return True
            'If Csla.ApplicationContext.User.IsInRole("<%= ObjectData.ClassName %>EditGroup") Then
            '	Return True
            'End If
            'Return False
        End Function

        Public Shared Function CanDeleteObject() As Boolean
            'TODO: Define CanDeleteObject permission in <%= ObjectData.ClassName %>
            Return True
            'If Csla.ApplicationContext.User.IsInRole("<%= ObjectData.ClassName %>DeleteGroup") Then
            '	Return True
            'End If
            'Return False
        End Function

#End Region ' Authorization Rules
</code>.Value

	End Function

	Private Function GenerateFactoryMethods() As String
		Return _
<code> 

    #Region " Factory Methods "

        Private Sub New()
            ' require use of factory method 
        End Sub

        Public Shared Function New<%= ObjectData.ClassName %>() As <%= ObjectData.ClassName %>
            If Not CanAddObject() Then
                Throw New System.Security.SecurityException("User not authorized to add a <%= ObjectData.ClassName %>")
            End If
            Return DataPortal.Create(Of <%= ObjectData.ClassName %>)()
        End Function

        Public Overloads Shared Function Get<%= ObjectData.ClassName %>(ByVal recipeId As Integer) As <%= ObjectData.ClassName %>
            If Not CanGetObject() Then
                Throw New System.Security.SecurityException("User not authorized to view a <%= ObjectData.ClassName %>")
            End If
            Return DataPortal.Fetch(Of <%= ObjectData.ClassName %>)(New Criteria(recipeId))
        End Function

        Public Shared Sub Delete<%= ObjectData.ClassName %>(ByVal recipeId As Integer)
            If Not CanDeleteObject() Then
                Throw New System.Security.SecurityException("User not authorized to remove a <%= ObjectData.ClassName %>")
            End If
            DataPortal.Delete(New Criteria(recipeId))
        End Sub

        Public Overrides Function Save() As <%= ObjectData.ClassName %>
            If IsDeleted AndAlso Not CanDeleteObject() Then
                Throw New System.Security.SecurityException("User not authorized to remove a <%= ObjectData.ClassName %>")
            ElseIf IsNew AndAlso Not CanAddObject() Then
                Throw New System.Security.SecurityException("User not authorized to add a <%= ObjectData.ClassName %>")
            ElseIf Not CanEditObject() Then
                Throw New System.Security.SecurityException("User not authorized to update a <%= ObjectData.ClassName %>")
            End If
            Return MyBase.Save()
        End Function

#End Region ' Factory Methods
</code>.Value

	End Function

End Class
